var util = require('../../../utils/util.js');
var wxcache = require('../../../utils/wxcache.js');
var api = require('../../../config/api.js');

var app = getApp();

Page({
  data: {
    allFeedbackType:null,
    array: [],
    index: 0,
    textArea:'',
    atype:'',
    phoneNumber:'',
    checkFlag:0,
    queryBean:{},
    imgList:[],
    imgFile:[],
    tempFilePaths:[],
    tempFiles: [],
    _isNumi0:0,
    _isNumi1: 0,
    _isNumi2: 0,
    _isNumi3: 0,
    _isNumi4: 0,
    anonymousComment:0,
  },
  onLoad: function (options) {
    var queryBean = JSON.parse(options.queryBeanStr);
    this.setData({
      queryBean: queryBean
    });
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  },
  bindTextAreaBlur: function (e) {
    //获取用户输入的反馈内容
    this.setData({
      textArea: e.detail.value
    })
  },
  addImg:function(){
    //添加图片
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#CED63A",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },
  chooseWxImage: function (type) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res);
        for(var i=0; i<res.tempFilePaths.length; i++){
          that.data.imgList.push(res.tempFilePaths[i]);
          that.data.imgFile.push(res.tempFiles[i]);
        }
        that.setData({
          tempFilePaths: that.data.imgList,
          tempFiles: that.data.imgFile,
        })
        console.log(that.data.tempFilePaths);
      }
    })
  },
  deleteImg:function(e){
    e.currentTarget.id;
    for (var i = 0; i < this.data.imgList.length; i++){
      if (i == e.currentTarget.id) {
        this.data.imgList.splice(i, 1);
        this.data.imgFile.splice(i, 1);
      }
    }
    this.setData({
      tempFilePaths: this.data.imgList,
      tempFiles: this.data.imgFile
    })
  },
  commitInfo: function () {
    var that = this;
    this.setData({
      checkFlag:0
    });
    this.checkInfo();
    if (this.data.checkFlag>0){
      return;
    }
    var num = that.data._isNumi0+that.data._isNumi1+that.data._isNumi2+that.data._isNumi3+that.data._isNumi4;
    var data = {};
    data.userId = wxcache.get('userInfo').id;
    data.commentUserName = wxcache.get('userInfo').nickName;
    data.anonymousComment = that.data.anonymousComment;
    data.campaignCommentContent = that.data.textArea;
    data.campaignId = that.data.queryBean.campaignId;
    data.orderId = that.data.queryBean.orderId;
    // data.pictureUrl = that.data.tempFilePaths;
    data.pictureUrl=[]
    data.campaignCommentGrade = num;
    
    // //批量图片上传
    // wx.showToast({
    //   icon: "loading",
    //   title: "正在上传图片"
    // });
     
   var count=0
    for(var i = 0; i < that.data.tempFilePaths.length; i++) {
      
      wx.uploadFile({
        url: api.PictureUpload,
        filePath: that.data.tempFilePaths[i],
        method:'POST',
        name: 'file',
        header: { "Content-Type": "multipart/form-data", 'r_s': 'emhvbmd5b3U='},
        success: function (res) {
          count++;
          console.log(res);
          
          if (res.statusCode != 200) {
            wx.showModal({
              title: '提示',
              content: '上传失败',
              showCancel: false
            })
            return;
          }else{
            var resbody = JSON.parse(res.data)
            data.pictureUrl.push(resbody.responseBody)
          }
   
        },
        fail: function (e) {
          console.log(e);
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
          return;
        },
        complete: function () {
           
          if (count == that.data.tempFilePaths.length) {
            // wx.hideToast();  //隐藏Toast
            console.log("图片已经上传完毕！")

            util.request(api.AddComment, data, 'POST').then(function (res) {
              console.log(res);
              if (res.code == 200) {
                console.log(res);
                wx.showModal({
                  title: '提示',
                  content: '已提交，感谢您的评价！！',
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定');
                      wx.redirectTo({
                        url: '/pages/ucenter/order/order?tab=4',
                      })

                    }
                  }
                })
              } else {
                wx.showModal({
                  title: '提示',
                  content: '提交失败，请联系管理员！',
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定');
                    } else {
                      console.log('用户点击取消');
                    }
                  }
                })
              }
            })
          }
          
        }
      }) 
    }

    console.log(data)
    

    

    
  },
  checkInfo:function(options){
    var that = this;
    var param = that.data.textArea;
      if (param == ""){
        that.data.checkFlag++;
        wx.showModal({
          title: '提示',
          content: '未输入信息',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定');
            } else {
              console.log('用户点击取消');
            }
          }
        })
      }
  },
  ns_0:function(){
     this.setData({
       _isNumi0:1,
       _isNumi1:0,
       _isNumi2:0,
       _isNumi3:0,
       _isNumi4:0,
     });
  },
  s_0: function () {
    this.setData({
      _isNumi0: 1,
      _isNumi1: 0,
      _isNumi2: 0,
      _isNumi3: 0,
      _isNumi4: 0,
    });
  },
  ns_1: function () {
    this.setData({
      _isNumi0: 1,
      _isNumi1: 1,
      _isNumi2: 0,
      _isNumi3: 0,
      _isNumi4: 0,
    });
  },
  s_1: function () {
    this.setData({
      _isNumi0: 1,
      _isNumi1: 1,
      _isNumi2: 0,
      _isNumi3: 0,
      _isNumi4: 0,
    });
  },
  ns_2: function () {
    this.setData({
      _isNumi0: 1,
      _isNumi1: 1,
      _isNumi2: 1,
      _isNumi3: 0,
      _isNumi4: 0,
    });
  },
  s_2: function () {
    this.setData({
      _isNumi0: 1,
      _isNumi1: 1,
      _isNumi2: 1,
      _isNumi3: 0,
      _isNumi4: 0,
    });
  },
  ns_3: function () {
    this.setData({
      _isNumi0: 1,
      _isNumi1: 1,
      _isNumi2: 1,
      _isNumi3: 1,
      _isNumi4: 0,
    });
  },
  s_3: function () {
    this.setData({
      _isNumi0: 1,
      _isNumi1: 1,
      _isNumi2: 1,
      _isNumi3: 1,
      _isNumi4: 0,
    });
  },
  ns_4: function () {
    this.setData({
      _isNumi0: 1,
      _isNumi1: 1,
      _isNumi2: 1,
      _isNumi3: 1,
      _isNumi4: 1,
    });
  },
  s_4: function () {
    this.setData({
      _isNumi0: 1,
      _isNumi1: 1,
      _isNumi2: 1,
      _isNumi3: 1,
      _isNumi4: 1,
    });
  },

  isAnonymous:function(){
    if (this.data.anonymousComment == 0){
      this.setData({
        anonymousComment:1
      }) 
      return
    }
    if (this.data.anonymousComment == 1) {
      this.setData({
        anonymousComment: 0
      })
      return
    }
  }


})